<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>7_猜数字练习</title>
    </head>
    <body>
        <h3>已生成一个1-100的随机数：</h3>
        <br>
        <input type="text">
        <button onclick="guess()">点我验证</button>
        <br>
        <h3>小提示：<span></span></h3>

        <script>
            let n = parseInt(Math.random()*100)+1;
            console.log(n);
            let count = 0;
            function guess(){
                let num = document.querySelector('input').value;
                if(!/^\d+$/.test(num)){
                    alert('请输入数字');
                }
                let spanE = document.querySelector('span');
                count++;
                if(num>n){
                    spanE.innerHTML = '猜大了！';
                }else if(num<n){
                    spanE.innerHTML = '猜小了！';
                }else {
                    spanE.innerHTML = '恭喜你用了'+count+'次就猜对了！'
                }
            }

        </script>
    </body>
</html>